package iPaint;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public class FireMonitor {

	public static ArrayList<MonitorMap> monitorMap = new ArrayList<MonitorMap>();
	public static int lineOfFlie = 0;
	public static int numOfMap = 0;
	public static int whichMap = 0;

	public static void readFromFile() {
		try {
			// Open the File in the unique location
			FileInputStream fsstream = new FileInputStream("C://data.txt");

			// Get the object of DataInputString
			DataInputStream in = new DataInputStream(fsstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;

			int tempZ;

			String[] tempCoordinateString;

			ArrayList<Integer> tempCoordinate = new ArrayList<Integer>();

			String tempMapFilePath;

			MonitorMap tempMonitorMap = new MonitorMap();

			// Read File Line By Line
			while ((strLine = br.readLine()) != null) {

				lineOfFlie++;

				if (lineOfFlie % 3 == 1) {
					tempZ = Integer.parseInt(strLine);
					tempMonitorMap.setZ(tempZ);
				}

				if (lineOfFlie % 3 == 2) {
					tempCoordinateString = strLine.split(" ");
					for (int i = 0; i < tempCoordinateString.length; i++) {
						tempCoordinate.add(Integer
								.parseInt(tempCoordinateString[i]));
					}
					tempMonitorMap.setCoordinate(tempCoordinate);
				}

				if (lineOfFlie > 0 && lineOfFlie % 3 == 0) {
					tempMapFilePath = strLine;
					tempMonitorMap.setMapFilePath(tempMapFilePath);
					MonitorMap temptempMonitorMap = new MonitorMap(
							tempMonitorMap);
					monitorMap.add(temptempMonitorMap);
					tempCoordinate = new ArrayList<Integer>();
					numOfMap++;
				}

			}
		} catch (Exception e) {
			System.err.println("Error:" + e.getMessage());
		}
	}

	public static void showFile(iPaint ip) {
		openImage(ip, whichMap);
		ip.zOfTheBuilding.setText(Integer.toString(monitorMap.get(whichMap)
				.getZ()));
		paintFireRound(ip, whichMap);

	}

	public static void openImage(iPaint ip, int tWhichMap) {
		try {
			File input = new File(monitorMap.get(tWhichMap).getMapFilePath());
			ip.bufferedImage = ImageIO.read(input);
		} catch (Exception e) {
			e.printStackTrace();
		}

		ip.drawPanel.repaint();
		ip.setSize(ip.bufferedImage.getWidth() + 400, ip.bufferedImage
				.getHeight() + 200);

	}

	public static void paintFireRound(iPaint ip, int tWhichMap) {

		ArrayList<Integer> tempCoordinate = new ArrayList<Integer>(monitorMap
				.get(tWhichMap).getCoordinate());

		Graphics2D gp = ip.bufferedImage.createGraphics();
		gp.setColor(Color.red);
		gp.setStroke(new BasicStroke(4));

		for (int i = 0; i < tempCoordinate.size() - 1; i = i + 2) {
			int x = tempCoordinate.get(i);
			int y = tempCoordinate.get(i + 1);
			gp.drawOval(x - 5, y - 5, 10, 10);
		}

		ip.drawPanel.repaint();
	}

	public static void next(iPaint ip) {

		whichMap++;
		if (whichMap >= numOfMap)
			whichMap = whichMap - numOfMap;
		System.out.println(whichMap);

		showFile(ip);
	}

}
